﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SPIWareMVVM.ViewModels;
using DataAccess;

namespace SPIWareMVVM.Views
{
    /// <summary>
    /// Interaction logic for ProductionList.xaml
    /// </summary>
    public partial class ProductionList : UserControl
    {
        Window1 mainWindow { get; set; }
        ProductionViewModel productionVM = new ProductionViewModel();
        bool _displayClosedWO { get; set; }
        public bool DisplayClosedWO
        {
            get { return _displayClosedWO; }
            set { _displayClosedWO = value; DisplayWOList(); }
        }

        public ProductionList()
        {
            InitializeComponent();

            DisplayClosedWO = false;
        }

        public void Production_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            mainWindow = (Window1)Window.GetWindow((ListBox)sender);
            mainWindow.HideLists();
            if (e.RemovedItems.Count > 0)
            {
                //productionVM.ResetRunTimeVariables();
            }
            if (e.AddedItems.Count > 0)
            {
                MachineProduction mp = (MachineProduction)e.AddedItems[0];
                if (mp != null)
                {
                    if (productionVM.lb == null) productionVM.lb = (ListBox)sender;
                    productionVM.SelectedRun = mp;

                    if (this.Visibility == Visibility.Collapsed) this.Visibility = Visibility.Visible;
                    this.dpProductionEdit.Visibility = System.Windows.Visibility.Visible;
                    this.cboOperators.ItemsSource = productionVM.Operators;
                }
            }
        }
        protected void PressMachine_Changed(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                MachinePress mp = (MachinePress)e.AddedItems[0];
                productionVM.SelectedMachine = mp;
                productionVM.SelectedRun.MachinePress = mp;
                this.cboWorkOrders.ItemsSource = productionVM.ocWorkOrders
                    .DisplayOnlyOpen()
                    .Where(m => m.Part.PressSize == mp.PressSize)
                    .OrderBy(c => c.WorkOrderId).ToObservableCollection<WorkOrder>();

                int size = 45;
                var test = productionVM.ocWorkOrders.Where(m => m.Part.PressSize == size);

                productionVM.SelectedRun.MachinePress = mp;

                e.Handled = true;
                ((ComboBox)sender).SelectedIndex = -1;
            }
        }
        protected void Shift_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0 && productionVM != null && productionVM.SelectedRun != null)
            {
                WorkShifts shift = (WorkShifts)e.AddedItems[0];
                if (shift != null)
                {
                    MachineProduction run = productionVM.SelectedRun;
                    run.Shift = (int)shift;

                    int year = ((DateTime)run.EndTime).Year;
                    int month = ((DateTime)run.EndTime).Month;
                    int day = ((DateTime)run.EndTime).Day;
                    switch ((int)shift)
                    {
                        case 0: run.StartTime = new DateTime(year, month, day, 0, 0, 0); run.EndTime = new DateTime(year, month, day, 8, 0, 0); break;
                        case 1: run.StartTime = new DateTime(year, month, day, 8, 0, 0); run.EndTime = new DateTime(year, month, day, 16, 0, 0); break;
                        case 2: run.StartTime = new DateTime(year, month, day, 16, 0, 0); run.EndTime = new DateTime(year, month, day, 0, 0, 0).AddDays(1); break;
                    }

                    productionVM.SelectedRun = run;

                    e.Handled = true;
                    ((ComboBox)sender).SelectedIndex = -1;
                }
            }
        }
        protected void WorkOrder_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0 && productionVM != null && productionVM.SelectedRun != null)
            {
                WorkOrder workOrder = (WorkOrder)e.AddedItems[0];
                if (workOrder != null)
                {
                    productionVM.SelectedRun.WorkOrder = workOrder;
                    productionVM.SelectedRun.Part = workOrder.Part;
                    //this.spLogHeader.DataContext = workOrder;
                }

                e.Handled = true;
                ((ComboBox)sender).SelectedIndex = -1;
            }
        }
        protected void Operator_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                //ApplicationUser user = (ApplicationUser)e.AddedItems[0];
                //if (user != null) productionVM.SelectedRun.ApplicationUser = user;

                ProductionUser pUser = (ProductionUser)e.AddedItems[0];
                if (pUser != null) productionVM.SelectedRun.ApplicationUser = pUser.User;

                e.Handled = true;
                ((ComboBox)sender).SelectedIndex = -1;
            }
        }

        private void btnViewClosedWO_Clicked(object sender, RoutedEventArgs e)
        {
            DisplayClosedWO = !DisplayClosedWO;
        }
        void DisplayWOList()
        {
            ObservableCollection<WorkOrder> list = ApplicationData.ocWorkOrders
                    .OrderBy(o => o.Customer.Name)
                    .ThenBy(o => o.Part.PartNumber).ToObservableCollection<WorkOrder>();

            if (!DisplayClosedWO) list = list.DisplayOnlyOpen()
                    .OrderBy(o => o.Customer.Name)
                    .ThenBy(o => o.Part.PartNumber).ToObservableCollection<WorkOrder>();

            this.cboWorkOrders.ItemsSource = list;
        }
    }
}
